package com.hutu.demo.config;

import lombok.Data;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.hutu.demo.RabbitMqApplication.USER_DEAD_EX;
import static com.hutu.demo.RabbitMqApplication.USER_DEAD_EX_QUEUE;
import static com.hutu.demo.RabbitMqApplication.USER_DEAD_EX_QUEUE_RK;
import static com.hutu.demo.RabbitMqApplication.USER_EX;
import static com.hutu.demo.RabbitMqApplication.USER_EX_QUEUE;
import static com.hutu.demo.RabbitMqApplication.USER_EX_QUEUE_RK;

@Configuration
@ConfigurationProperties("spring.rabbitmq.listener.simple")
@Data
public class RabbitConfig {
    private int prefetch;
    private int batchSize;
    private boolean consumerBatchEnabled;
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        //  交换机
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (ack) {
                System.out.println("Message successfully sent!");
            } else {
                System.out.println("Message lost!");
            }
//            System.out.println(correlationData.getId());
        });
        //  队列
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            System.out.println("Returned message: " + message);
        });
        System.out.println("==================");
        return rabbitTemplate;
    }

//    @Bean
//    public Queue queueSportsGossip() {
//        // 设置队列的 TTL 为 60000 毫秒（1 分钟）
//        return QueueBuilder.durable("queue_test_timout_2")
//                .ttl(10000)
//                .build();
//    }
//
//    @Bean
//    public DirectExchange newsExchange() {
//        return new DirectExchange("news_exchange");
//    }
//
//    @Bean
//    public Binding binding(Queue queueSportsGossip, DirectExchange newsExchange) {
//        return BindingBuilder.bind(queueSportsGossip)
//                .to(newsExchange)
//                .with("timeout_2");
//    }


/*    @Bean
    public SimpleRabbitListenerContainerFactory cusSimpleRabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setPrefetchCount(prefetch);
        factory.setBatchSize(batchSize);
        factory.setConsumerBatchEnabled(consumerBatchEnabled);
        factory.setBatchListener(true); // 设置批量监听
        return factory;
    }*/

/*    @Bean
    public String cusSimpleRabbitListenerContainerFactory(SimpleRabbitListenerContainerFactory containerFactory) {
        containerFactory.setBatchListener(true);
        return "cusSimpleRabbitListenerContainerFactory";
    }*/



    //    正常交换机 begin
    @Bean
    public DirectExchange userDirectExchange(){
        return new DirectExchange(USER_EX);
    }
    // 重点参考点
    @Bean
    public Queue userQueue() {
        // 设置队列的 TTL 为 10000 毫秒（10s）
        return QueueBuilder.durable(USER_EX_QUEUE)
                .ttl(10000)
                .deadLetterExchange(USER_DEAD_EX)
                .deadLetterRoutingKey(USER_DEAD_EX_QUEUE_RK)
                .maxLength(100)
                .build();
    }
    @Bean
    public Binding binding(Queue userQueue, DirectExchange userDirectExchange) {
        return BindingBuilder.bind(userQueue)
                .to(userDirectExchange)
                .with(USER_EX_QUEUE_RK);
    }
//   正常交换机 end

    //    死信交换机 begin
    @Bean
    public DirectExchange userDeadDirectExchange(){
        return new DirectExchange(USER_DEAD_EX);
    }
    @Bean
    public Queue userDeadQueue() {
        return QueueBuilder.durable(USER_DEAD_EX_QUEUE)
                .build();
    }
    @Bean
    public Binding userDeadBinding(Queue userDeadQueue, DirectExchange userDeadDirectExchange) {
        return BindingBuilder.bind(userDeadQueue)
                .to(userDeadDirectExchange)
                .with(USER_DEAD_EX_QUEUE_RK);
    }
}
